import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {saleOrderForecastStatus, salePlanAssembleMethod, saleOrderForecastSummaryWay} from '../Enum';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        const {brands} = this.props.initEnumData;
        const {pageIndex, pageSize, total} = this.props;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1SalePlanQuantity',
                defaultMessage: 'N+1月订单计划数'
            }),
            dataIndex: 'n1SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1ForecastOrderQuantity',
                defaultMessage: 'N+1月建议订单数'
            }),
            dataIndex: 'n1ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1BeginningInventory',
                defaultMessage: 'N+1月期初库存数'
            }),
            dataIndex: 'n1BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1EndingInventory',
                defaultMessage: 'N+1月期末库存数'
            }),
            dataIndex: 'n1EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1OrderPlanQuantity',
                defaultMessage: 'N+1月销售计划数'
            }),
            dataIndex: 'n1OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1TheoreticalStoreSaleRatio',
                defaultMessage: 'N+1月理论存销比'
            }),
            dataIndex: 'n1TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n1ActualStoreSaleRatio',
                defaultMessage: 'N+1月实际存销比'
            }),
            dataIndex: 'n1ActualStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2SalePlanQuantity',
                defaultMessage: 'N+2月订单计划数'
            }),
            dataIndex: 'n2SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2ForecastOrderQuantity',
                defaultMessage: 'N+2月建议订单数'
            }),
            dataIndex: 'n2ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2BeginningInventory',
                defaultMessage: 'N+2月期初库存数'
            }),
            dataIndex: 'n2BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2EndingInventory',
                defaultMessage: 'N+2月期末库存数'
            }),
            dataIndex: 'n2EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2OrderPlanQuantity',
                defaultMessage: 'N+2月销售计划数'
            }),
            dataIndex: 'n2OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2TheoreticalStoreSaleRatio',
                defaultMessage: 'N+2月理论存销比'
            }),
            dataIndex: 'n2TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n2ActualStoreSaleRatio',
                defaultMessage: 'N+2月实际存销比'
            }),
            dataIndex: 'n2ActualStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3SalePlanQuantity',
                defaultMessage: 'N+3月订单计划数'
            }),
            dataIndex: 'n3SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3ForecastOrderQuantity',
                defaultMessage: 'N+3月建议订单数'
            }),
            dataIndex: 'n3ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3BeginningInventory',
                defaultMessage: 'N+3月期初库存数'
            }),
            dataIndex: 'n3BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3EndingInventory',
                defaultMessage: 'N+3月期末库存数'
            }),
            dataIndex: 'n3EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3OrderPlanQuantity',
                defaultMessage: 'N+3月销售计划数'
            }),
            dataIndex: 'n3OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3TheoreticalStoreSaleRatio',
                defaultMessage: 'N+3月理论存销比'
            }),
            dataIndex: 'n3TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n3ActualStoreSaleRatio',
                defaultMessage: 'N+3月实际存销比'
            }),
            dataIndex: 'n3ActualStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4SalePlanQuantity',
                defaultMessage: 'N+4月订单计划数'
            }),
            dataIndex: 'n4SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4ForecastOrderQuantity',
                defaultMessage: 'N+4月建议订单数'
            }),
            dataIndex: 'n4ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4BeginningInventory',
                defaultMessage: 'N+4月期初库存数'
            }),
            dataIndex: 'n4BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4EndingInventory',
                defaultMessage: 'N+4月期末库存数'
            }),
            dataIndex: 'n4EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4OrderPlanQuantity',
                defaultMessage: 'N+4月销售计划数'
            }),
            dataIndex: 'n4OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4TheoreticalStoreSaleRatio',
                defaultMessage: 'N+4月理论存销比'
            }),
            dataIndex: 'n4TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n4ActualStoreSaleRatio',
                defaultMessage: 'N+4月实际存销比'
            }),
            dataIndex: 'n4ActualStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5SalePlanQuantity',
                defaultMessage: 'N+5月订单计划数'
            }),
            dataIndex: 'n5SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5ForecastOrderQuantity',
                defaultMessage: 'N+5月建议订单数'
            }),
            dataIndex: 'n5ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5BeginningInventory',
                defaultMessage: 'N+5月期初库存数'
            }),
            dataIndex: 'n5BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5EndingInventory',
                defaultMessage: 'N+5月期末库存数'
            }),
            dataIndex: 'n5EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5OrderPlanQuantity',
                defaultMessage: 'N+5月销售计划数'
            }),
            dataIndex: 'n5OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5TheoreticalStoreSaleRatio',
                defaultMessage: 'N+5月理论存销比'
            }),
            dataIndex: 'n5TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n5ActualStoreSaleRatio',
                defaultMessage: 'N+5月实际存销比'
            }),
            dataIndex: 'n5ActualStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6SalePlanQuantity',
                defaultMessage: 'N+6月订单计划数'
            }),
            dataIndex: 'n6SalePlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6ForecastOrderQuantity',
                defaultMessage: 'N+6月建议订单数'
            }),
            dataIndex: 'n6ForecastOrderQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6BeginningInventory',
                defaultMessage: 'N+6月期初库存数'
            }),
            dataIndex: 'n6BeginningInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6EndingInventory',
                defaultMessage: 'N+6月期末库存数'
            }),
            dataIndex: 'n6EndingInventory',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6OrderPlanQuantity',
                defaultMessage: 'N+6月销售计划数'
            }),
            dataIndex: 'n6OrderPlanQuantity',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6TheoreticalStoreSaleRatio',
                defaultMessage: 'N+6月理论存销比'
            }),
            dataIndex: 'n6TheoreticalStoreSaleRatio',
        },
        {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.n6ActualStoreSaleRatio',
                defaultMessage: 'N+6月实际存销比'
            }),
            dataIndex: 'n6ActualStoreSaleRatio',
        }];
        if(brands.length >= 1 && !this.props.conditions.saleOrderForecastSummaryWay)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.brandName',
                        defaultMessage: '品牌'
                    }),
                    dataIndex: 'brandName',
                });
        if(!this.props.conditions.saleOrderForecastSummaryWay)
            columns.splice(1, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.marketDepartment',
                        defaultMessage: '大区'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'marketDepartment' && this.props.sortedOrder,
                    dataIndex: 'marketDepartment',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.country',
                        defaultMessage: '国家'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'country' && this.props.sortedOrder,
                    dataIndex: 'country',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerCode',
                        defaultMessage: '总代编号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerName',
                        defaultMessage: '总代名称'
                    }),
                    dataIndex: 'dealerName',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.manufacturerCode',
                        defaultMessage: '生产工厂编号'
                    }),
                    dataIndex: 'manufacturerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'manufacturerCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.manufacturerName',
                        defaultMessage: '生产工厂名称'
                    }),
                    dataIndex: 'manufacturerName',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastYear',
                        defaultMessage: '预测年份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastYear' && this.props.sortedOrder,
                    dataIndex: 'forecastYear',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastMonth',
                        defaultMessage: '预测月份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastMonth' && this.props.sortedOrder,
                    dataIndex: 'forecastMonth',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.assembleMethod',
                        defaultMessage: '装配方式'
                    }),
                    dataIndex: 'assembleMethod',
                    render: text => conventEnumValueToString(salePlanAssembleMethod, text)
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.materialGroupKDBOMCode',
                        defaultMessage: '物料组号或KDBOM编号'
                    }),
                    dataIndex: 'materialGroupKDBOMCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'materialGroupKDBOMCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.referenceMaterialGroupKDBOMCode',
                        defaultMessage: '参考物料组号或KDBOM编号'
                    }),
                    dataIndex: 'referenceMaterialGroupKDBOMCode',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.modelCode',
                        defaultMessage: '车型编号'
                    }),
                    dataIndex: 'modelCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'modelCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.modelName',
                        defaultMessage: '车型名称'
                    }),
                    dataIndex: 'modelName',
                });
        if(this.props.conditions.saleOrderForecastSummaryWay === saleOrderForecastSummaryWay.按大区)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.marketDepartment',
                        defaultMessage: '大区'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'marketDepartment' && this.props.sortedOrder,
                    dataIndex: 'marketDepartment',
                });
        if(this.props.conditions.saleOrderForecastSummaryWay === saleOrderForecastSummaryWay.按国家)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.country',
                        defaultMessage: '国家'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'country' && this.props.sortedOrder,
                    dataIndex: 'country',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastYear',
                        defaultMessage: '预测年份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastYear' && this.props.sortedOrder,
                    dataIndex: 'forecastYear',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastMonth',
                        defaultMessage: '预测月份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastMonth' && this.props.sortedOrder,
                    dataIndex: 'forecastMonth',
                });
        if(this.props.conditions.saleOrderForecastSummaryWay === saleOrderForecastSummaryWay.按总代)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerCode',
                        defaultMessage: '总代编号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerName',
                        defaultMessage: '总代名称'
                    }),
                    dataIndex: 'dealerName',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastYear',
                        defaultMessage: '预测年份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastYear' && this.props.sortedOrder,
                    dataIndex: 'forecastYear',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastMonth',
                        defaultMessage: '预测月份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastMonth' && this.props.sortedOrder,
                    dataIndex: 'forecastMonth',
                });
        if(this.props.conditions.saleOrderForecastSummaryWay === saleOrderForecastSummaryWay.按物料组号或KDBOM编号)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.materialGroupKDBOMCode',
                        defaultMessage: '物料组号或KDBOM编号'
                    }),
                    dataIndex: 'materialGroupKDBOMCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'materialGroupKDBOMCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastYear',
                        defaultMessage: '预测年份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastYear' && this.props.sortedOrder,
                    dataIndex: 'forecastYear',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastMonth',
                        defaultMessage: '预测月份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastMonth' && this.props.sortedOrder,
                    dataIndex: 'forecastMonth',
                });
        if(this.props.conditions.saleOrderForecastSummaryWay === saleOrderForecastSummaryWay.按车型)
            columns.splice(0, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.modelCode',
                        defaultMessage: '车型编号'
                    }),
                    dataIndex: 'modelCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'modelCode' && this.props.sortedOrder,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastYear',
                        defaultMessage: '预测年份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastYear' && this.props.sortedOrder,
                    dataIndex: 'forecastYear',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.forecastMonth',
                        defaultMessage: '预测月份'
                    }),
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'forecastMonth' && this.props.sortedOrder,
                    dataIndex: 'forecastMonth',
                });
        if(!this.props.conditions.saleOrderForecastSummaryWay)
            columns.splice(65, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.status',
                        defaultMessage: '状态'
                    }),
                    dataIndex: 'status',
                    render: text => conventEnumValueToString(saleOrderForecastStatus, text)
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.remark',
                        defaultMessage: '备注'
                    }),
                    dataIndex: 'remark',
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.createTime',
                        defaultMessage: '创建时间'
                    }),
                    dataIndex: 'createTime',
                    render: text => formatDateTime(text, DATETIME_FORMAT)
                });
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="id"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
import {injectIntl} from './intl';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: item.name,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    data: getData(state),
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));


